<!DOCTYPE HTML>
<html lang="en-US" manifest="./manifest.appcache">
    
    <head>
        
        <meta charset="UTF-8">
        <title>页面类型 | Pager</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 0.7.0">
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
        
    
    
    
    <link rel="next" href="./25.html" />
    
    
    <link rel="prev" href="./23.html" />
    

        
    </head>
    <body>
        
        
<link rel="stylesheet" href="gitbook/style.css">


        
    <div class="book"  data-level="0.2.4" data-basepath="." data-revision="1421113562133">
    <div class="book-header">
    <!-- Actions Left -->
    <a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>
    
    <a href="https://github.com/null" target="_blank" class="btn pull-left home-bookmark" aria-label="GitHub home"><i class="fa fa-bookmark-o"></i></a>
    
    <a href="#" class="btn pull-left toggle-search" aria-label="Toggle search"><i class="fa fa-search"></i></a>
    <span id="font-settings-wrapper">
        <a href="#" class="btn pull-left toggle-font-settings" aria-label="Toggle font settings"><i class="fa fa-font"></i>
        </a>
        <div class="dropdown-menu font-settings">
    <div class="dropdown-caret">
        <span class="caret-outer"></span>
        <span class="caret-inner"></span>
    </div>

    <div class="btn-group btn-block">
        <button id="reduce-font-size" class="btn btn-default">A</button>
        <button id="enlarge-font-size" class="btn btn-default">A</button>
    </div>

    <ul class="list-group font-family-list">
        <li class="list-group-item" data-font="0">Serif</li>
        <li class="list-group-item" data-font="1">Sans</li>
    </ul>

    <div class="btn-group btn-group-xs btn-block color-theme-list">
        <button type="button" class="btn btn-default" id="color-theme-preview-0" data-theme="0">White</button>
        <button type="button" class="btn btn-default" id="color-theme-preview-1" data-theme="1">Sepia</button>
        <button type="button" class="btn btn-default" id="color-theme-preview-2" data-theme="2">Night</button>
    </div>
</div>

    </span>

    <!-- Actions Right -->
    
    <a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Share on Google Plus"><i class="fa fa-google-plus"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Share on Facebook"><i class="fa fa-facebook"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Share on Twitter"><i class="fa fa-twitter"></i></a>
    
    

    <!-- Title -->
    <h1>
        <i class="fa fa-spinner fa-spin"></i>
        <a href="./" >Pager</a>
    </h1>
</div>

    

<div class="book-summary">
    <div class="book-search">
        <input type="text" placeholder="Search" class="form-control" />
    </div>
    <ul class="summary">
        
        

        

        

	

        

        
    
        
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="./index.html">
                        <i class="fa fa-check"></i>
                        
                         Pager
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="0.1" data-path="pagersqlite.html">
            
                
                    <a href="./pagersqlite.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.1.</b>
                        
                         Pager在SQLite中的地位及作用
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="0.2" data-path="pager.html">
            
                
                    <a href="./pager.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.2.</b>
                        
                         Pager概述
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="0.2.1" data-path="21.html">
            
                
                    <a href="./21.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.2.1.</b>
                        
                         Pager状态
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="0.2.2" data-path="22.html">
            
                
                    <a href="./22.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.2.2.</b>
                        
                         Pager的数据库文件页
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="0.2.3" data-path="23.html">
            
                
                    <a href="./23.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.2.3.</b>
                        
                         Pager的相关参数
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter active" data-level="0.2.4" data-path="24.html">
            
                
                    <a href="./24.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.2.4.</b>
                        
                         页面类型
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="0.2.5" data-path="25.html">
            
                
                    <a href="./25.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.2.5.</b>
                        
                         Pager的状态
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="0.2.6" data-path="26.html">
            
                
                    <a href="./26.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.2.6.</b>
                        
                         Pager的结构
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="0.2.7" data-path="27.html">
            
                
                    <a href="./27.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.2.7.</b>
                        
                         日志
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="0.2.8" data-path="28.html">
            
                
                    <a href="./28.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.2.8.</b>
                        
                         页面缓存
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="0.2.9" data-path="29.html">
            
                
                    <a href="./29.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.2.9.</b>
                        
                         锁机制
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        
        <li class="chapter " data-level="0.3" data-path="sqlitepager.html">
            
                
                    <a href="./sqlitepager.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.3.</b>
                        
                         SQLite中Pager的运行流程概述
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="0.3.1" data-path="31.html">
            
                
                    <a href="./31.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.3.1.</b>
                        
                         打开/关闭一个页面连接
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="0.3.2" data-path="32.html">
            
                
                    <a href="./32.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.3.2.</b>
                        
                         配置页面对象的函数
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="0.3.3" data-path="33.html">
            
                
                    <a href="./33.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.3.3.</b>
                        
                         获取和释放页面引用的函数
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="0.3.4" data-path="34.html">
            
                
                    <a href="./34.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.3.4.</b>
                        
                         对页面引用的操作
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="0.3.5" data-path="35.html">
            
                
                    <a href="./35.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.3.5.</b>
                        
                         管理页面事务和保存点的函数
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="0.3.6" data-path="36.html">
            
                
                    <a href="./36.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.3.6.</b>
                        
                         查询页面状态/配置
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="0.3.7" data-path="37.html">
            
                
                    <a href="./37.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.3.7.</b>
                        
                         截断数据库文件
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="0.3.8" data-path="38.html">
            
                
                    <a href="./38.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.3.8.</b>
                        
                         测试/调试
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    


        
        <li class="divider"></li>
        <li>
            <a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
        </li>
        
    </ul>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="page-wrapper" tabindex="-1">
                <div class="book-progress">
    <div class="bar">
        <div class="inner" style="width: 30%;min-width: 25%;"></div>
    </div>
    <div class="chapters">
    
        <a href="./index.html" title="Pager" class="chapter done new-chapter" data-progress="0" style="left: 0%;"></a>
    
        <a href="./pagersqlite.html" title="Pager在SQLite中的地位及作用" class="chapter done " data-progress="0.1" style="left: 5%;"></a>
    
        <a href="./pager.html" title="Pager概述" class="chapter done " data-progress="0.2" style="left: 10%;"></a>
    
        <a href="./21.html" title="Pager状态" class="chapter done " data-progress="0.2.1" style="left: 15%;"></a>
    
        <a href="./22.html" title="Pager的数据库文件页" class="chapter done " data-progress="0.2.2" style="left: 20%;"></a>
    
        <a href="./23.html" title="Pager的相关参数" class="chapter done " data-progress="0.2.3" style="left: 25%;"></a>
    
        <a href="./24.html" title="页面类型" class="chapter done " data-progress="0.2.4" style="left: 30%;"></a>
    
        <a href="./25.html" title="Pager的状态" class="chapter  " data-progress="0.2.5" style="left: 35%;"></a>
    
        <a href="./26.html" title="Pager的结构" class="chapter  " data-progress="0.2.6" style="left: 40%;"></a>
    
        <a href="./27.html" title="日志" class="chapter  " data-progress="0.2.7" style="left: 45%;"></a>
    
        <a href="./28.html" title="页面缓存" class="chapter  " data-progress="0.2.8" style="left: 50%;"></a>
    
        <a href="./29.html" title="锁机制" class="chapter  " data-progress="0.2.9" style="left: 55%;"></a>
    
        <a href="./sqlitepager.html" title="SQLite中Pager的运行流程概述" class="chapter  " data-progress="0.3" style="left: 60%;"></a>
    
        <a href="./31.html" title="打开/关闭一个页面连接" class="chapter  " data-progress="0.3.1" style="left: 65%;"></a>
    
        <a href="./32.html" title="配置页面对象的函数" class="chapter  " data-progress="0.3.2" style="left: 70%;"></a>
    
        <a href="./33.html" title="获取和释放页面引用的函数" class="chapter  " data-progress="0.3.3" style="left: 75%;"></a>
    
        <a href="./34.html" title="对页面引用的操作" class="chapter  " data-progress="0.3.4" style="left: 80%;"></a>
    
        <a href="./35.html" title="管理页面事务和保存点的函数" class="chapter  " data-progress="0.3.5" style="left: 85%;"></a>
    
        <a href="./36.html" title="查询页面状态/配置" class="chapter  " data-progress="0.3.6" style="left: 90%;"></a>
    
        <a href="./37.html" title="截断数据库文件" class="chapter  " data-progress="0.3.7" style="left: 95%;"></a>
    
        <a href="./38.html" title="测试/调试" class="chapter  " data-progress="0.3.8" style="left: 100%;"></a>
    
    </div>
</div>

                <div class="page-inner">
                
                    <section class="normal" id="section-gitbook_51">
                    
                        <h1 id="">页面类型</h1>
<p>Sqlite中的页面类型包括：</br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;叶子页面(leaf)：存储数据;</br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;内部页面(internal): 包含查询时的导航信息；</br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;溢出页面(overflow)；</br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;空闲页面(free)；</br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;（1）.    file header</br>
文件以1号页面开始，它包括100个字节的文件头。</br>
Header string(头字符串)：16个字节，&quot;SQLite format 3.&quot;</br>
Page size（页面大小）：1024</br>
File format（文件格式）：在当前的版本都为1。</br>
Reserved space（保留空间）：1个字节，SQLite在每个页面的末尾都会保留一定的空间，留作它用，默认为0。</br>
Embedded payload：max embedded payload fraction(偏移21)的值限定了B树内节点（页面）中一个元组（记录，单元）最多能够使用的空间。255意味着100%，默认值为0x40，即64（25%），这保证了一个结点（页面）至少有4个单元。如果一个单元的负载(payload，即数据量)超过最大值，则溢出的数据保存到溢出的页面，一旦SQLite分配了一个溢出页面，它会尽可能多的移动数据到溢出页面，下限为min embedded payload fraction value（偏移为22），默认的值为32，即12.5% 。</br>
File change counter（文件修改计数）：通常被事务使用，它由事务增加其值。该值的主要目的是数据库改变时，pager避免对缓存进行刷盘。</br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Freelist（空闲页面链表）：在文件头偏移32的4个字节记录着空闲页面链的第一个页面，偏移36处的4个字节为空闲页面的数量。</br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;空闲页面分为两种页面：trunk pages（主页面）和leaf  pages(叶子页面)。文件头的指针指向空闲链表的第一个trunk page，每个trunk page指向多个叶子页面。</br>
Trunk page的格式如下，从页面的起始处开始：</br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4个字节，指向下一个trunk page的页面号；</br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4个字节，该页面的叶子页面指针的数量；</br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;指向叶子页面的页面号，每项4个字节。</br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Meta variables（元数据变量）：</br>
从偏移为40开始，为15个4字节的元数据变量，这些元数据主要与B树和VM有关。</br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;（2）.    读取file</br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;程序调用API sqlite3_open打开数据库文件时，SQLite就会读取文件头进行数据库的初始化。</br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;数据库文件页的可复写条件：</br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;①在事务开始的时候，页面的源文件就应该被写入回滚日志并同步。</br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;②pager在事务开始的时候是freelist的叶子页面。</br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;③在事务开始的时候，页码要大于数据库文件中的最大页。</br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;当然数据库文件页也有一些不可复写的，除了①某个页面同其他页面在同一个扇区。②原子页的写优化是可行的，整个事务除了事务序列号的更新，都由一个单一的页变化组成。</br></p>

                    
                    </section>
                
                </div>
            </div>
        </div>

        
        <a href="./23.html" class="navigation navigation-prev " aria-label="Previous page: Pager的相关参数"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="./25.html" class="navigation navigation-next " aria-label="Next page: Pager的状态"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
<script src="gitbook/app.js"></script>

    
    <script src="https://cdn.mathjax.org/mathjax/2.0-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-mathjax/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
